package com.xm.xmapp.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.AuthorizeActivity;
import com.xm.xmapp.AuthorizeCompleteActivity;
import com.xm.xmapp.AuthorizeVerifyActivity;
import com.xm.xmapp.AuthorizeZhimaIsActivity;
import com.xm.xmapp.BigCustomerActivity;
import com.xm.xmapp.ChangeUserInfoActivity;
import com.xm.xmapp.CompanyUserActivity;
import com.xm.xmapp.FeedbackActivity;
import com.xm.xmapp.LoginActivity;
import com.xm.xmapp.MainActivity;
import com.xm.xmapp.MyOrderActivity;
import com.xm.xmapp.MyShareActivity;
import com.xm.xmapp.MyWalletActivity;
import com.xm.xmapp.QuestionListActivity;
import com.xm.xmapp.R;
import com.xm.xmapp.SettingActivity;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.app.Type.F_Main;
import com.xm.xmapp.base.BaseFragment;
import com.xm.xmapp.dialog.GuideDialog;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.myinterface.MainActivityInterFace;
import com.xm.xmapp.request.bean.Get_big_customer_invite_infoRequest;
import com.xm.xmapp.request.bean.Get_user_infoRequest;
import com.xm.xmapp.request.bean.Get_user_information_statusRequest;
import com.xm.xmapp.response.bean.CustomHeightBean;
import com.xm.xmapp.response.bean.ResponseBigCustomerInviteInfo;
import com.xm.xmapp.response.bean.ResponseUserBean;
import com.xm.xmapp.response.bean.UserInformationStatusBean;
import com.xm.xmapp.tools.Tools;

/**
 * @date 2014/11/14
 * @author wuwenjie
 * @description 侧边栏菜
 */
public class MainLeftFragment extends BaseFragment implements OnClickListener {

	private TextView qiehuan, order, phone, mywallet, tuijian, problem, setting, status, qiye_status, feedback;
	private ImageView headimg, fengexian;
	private RelativeLayout renzheng, qiye;
	private ResponseBigCustomerInviteInfo bean;

	private boolean isQiye = false;
	private TextView xinshou, zhima;
	private UserInformationStatusBean statusBean = new UserInformationStatusBean();
	
	Intent intent;
	ResponseUserBean userbean;

	@Override
	public int setContentView() {
		return R.layout.fragment_leftmain;
	}

	@Override
	public void initView() {

		qiehuan = (TextView) findViewById(R.id.qiehuan);
		order = (TextView) findViewById(R.id.order);
		phone = (TextView) findViewById(R.id.phone);
		mywallet = (TextView) findViewById(R.id.mywallet);
		renzheng = (RelativeLayout) findViewById(R.id.renzheng);
		qiye = (RelativeLayout) findViewById(R.id.qiye);
		status = (TextView) findViewById(R.id.status);
		qiye_status = (TextView) findViewById(R.id.qiye_status);
		tuijian = (TextView) findViewById(R.id.tuijian);
		problem = (TextView) findViewById(R.id.problem);
		setting = (TextView) findViewById(R.id.setting);
		xinshou = (TextView) findViewById(R.id.xinshou);
		zhima = (TextView) findViewById(R.id.zhima);
		feedback = (TextView) findViewById(R.id.feedback);
		headimg = (ImageView) findViewById(R.id.headimg);
		fengexian = (ImageView) findViewById(R.id.fengexian);
		initData();
	}

	@Override
	public void initData() {

		isQiye = false;
		ResponseUserBean bean = MyAppcation.getMyAppcation().getUserBean();
		if (bean != null) {
			if (bean.user_nickname == null || bean.user_nickname.equals("")) {
				String user_phone = bean.user_phone + "";
				user_phone = user_phone.substring(0, 3) + "****" + user_phone.substring(7, 11);
				phone.setText(user_phone + "");
			} else {
				phone.setText(bean.user_nickname);
			}
			if (bean.is_big_customer_user == 0) {
				fengexian.setVisibility(View.GONE);
				qiehuan.setVisibility(View.GONE);
			} else {
				fengexian.setVisibility(View.VISIBLE);
				qiehuan.setVisibility(View.VISIBLE);
			}
			statusBean.user_information_status = bean.check_data_status;
			// 1=审核通过，2=待审核，0=审核不通过',
			if (bean.check_data_status != 1)
				get_user_information_statusRequest();
			else {
				status.setText("已认证");
			}
			get_big_customer_invite_info(false);
			getUserInfo();
		} else {
			status.setText("未登录");
			qiye_status.setText("未登录");
			status.setTextColor(Color.parseColor("#999999"));
			qiye_status.setTextColor(Color.parseColor("#999999"));
			fengexian.setVisibility(View.GONE);
			qiehuan.setVisibility(View.GONE);
		}

	}

	private void getUserInfo() {
		Get_user_infoRequest requestBean = new Get_user_infoRequest();
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				ResponseUserBean base = JSON.parseObject(response, ResponseUserBean.class);
				if (base.user_nickname == null || base.user_nickname.equals("")) {
					String user_phone = base.user_phone + "";
					user_phone = user_phone.substring(0, 3) + "****" + user_phone.substring(7, 11);
					phone.setText(user_phone + "");
				} else
					phone.setText(base.user_nickname);

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});

	}

	private void get_user_information_statusRequest() {

		Get_user_information_statusRequest requestBean = new Get_user_information_statusRequest();
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				// user-information-status = 1 则为通过
				// user-information-status =0 则为不通过
				// user-information-status =2 则为待审核
				// user-information-status =-1 则为未上传任何资料（待认证）
				promptDialog.dismiss();
				statusBean = JSON.parseObject(response, UserInformationStatusBean.class);
				switch (statusBean.user_information_status) {
				case -1:
					status.setText("待认证");
					status.setTextColor(Color.parseColor("#f66767"));
					break;
				case 0:
					status.setText("未通过");
					status.setTextColor(Color.parseColor("#f66767"));
					break;
				case 1:
					status.setText("已认证");
					status.setTextColor(Color.parseColor("#999999"));
					break;
				case 2:
					status.setText("待审核");
					status.setTextColor(Color.parseColor("#999999"));
					break;
				default:
					break;
				}

			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});
	}

	public void get_big_customer_invite_info(final boolean isIntent) {
		promptDialog.show();
		Get_big_customer_invite_infoRequest requestBean = new Get_big_customer_invite_infoRequest();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				bean = JSON.parseObject(response, ResponseBigCustomerInviteInfo.class);
				if (bean.join_big_customer == -1) {
					qiye_status.setText("新的邀请!");
					qiye_status.setTextColor(Color.parseColor("#ff0000"));
				} else {
					qiye_status.setText("");
				}
				if (isIntent) {
					intent = new Intent(MyAppcation.getMyAppcation(), BigCustomerActivity.class);
					intent.putExtra("bean", bean);
					startActivity(intent);
				}
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {

				promptDialog.dismiss();
				if (userbean.is_big_customer_user != 0) {
					intent = new Intent(MyAppcation.getMyAppcation(), BigCustomerActivity.class);
				} else {
					intent = new Intent(MyAppcation.getMyAppcation(), CompanyUserActivity.class);
				}
				startActivity(intent);
			}
		});
	}

	@Override
	public void initEvent() {

		qiehuan.setOnClickListener(this);
		order.setOnClickListener(this);

		headimg.setOnClickListener(this);
		mywallet.setOnClickListener(this);
		renzheng.setOnClickListener(this);
		qiye.setOnClickListener(this);
		tuijian.setOnClickListener(this);
		problem.setOnClickListener(this);
		setting.setOnClickListener(this);
		xinshou.setOnClickListener(this);
		zhima.setOnClickListener(this);
		feedback.setOnClickListener(this);
	}


	@Override
	public void onClick(View v) {

		userbean = MyAppcation.getMyAppcation().getUserBean();
		if (userbean != null) {
			switch (v.getId()) {
			case R.id.feedback:
				intent = new Intent(MyAppcation.getMyAppcation(), FeedbackActivity.class);
				startActivity(intent);
				break;

			case R.id.xinshou:
				GuideDialog guideDialog = new GuideDialog(getActivity());
				guideDialog.show();
				break;

			case R.id.zhima:
				intent = new Intent(MyAppcation.getMyAppcation(), AuthorizeZhimaIsActivity.class);
				startActivity(intent);
				break;

			case R.id.headimg:
				intent = new Intent(MyAppcation.getMyAppcation(), ChangeUserInfoActivity.class);
				startActivity(intent);

				break;
			case R.id.qiehuan:
				isQiye = !isQiye;
				if (isQiye) {
					qiehuan.setText("切换个人用车");
				} else {
					qiehuan.setText("切换企业用车");
				}
				Tools.showMessage("正在切换");
				CustomHeightBean heightBean = new CustomHeightBean();
				heightBean.flag = MainActivityInterFace.CHANGE_TO_BIGCUSTOM_AND_COMMOM;
				MyAppcation.getMyAppcation().notifyDataSetChanged(MainActivity.class, Type.A_Main.CHANGE_UI_CODE,
						heightBean);
				break;
			case R.id.order:

				intent = new Intent(MyAppcation.getMyAppcation(), MyOrderActivity.class);
				startActivity(intent);
				break;

			case R.id.mywallet:
				intent = new Intent(MyAppcation.getMyAppcation(), MyWalletActivity.class);
				startActivity(intent);
				break;
			case R.id.renzheng:
				// state = -0;
				/**
				 * 认证成功，直接跳认证成功页面，否则请求一次获取认证状态的数据
				 */

				if (statusBean != null) {

					switch (statusBean.user_information_status) {
					case 1:
						intent = new Intent(MyAppcation.getMyAppcation(), AuthorizeCompleteActivity.class);
						startActivity(intent);
						break;
					case -1:
					case 0:
					case 2:
						startIntentToActivity();
						break;

					default:
						break;
					}
				} else {
					startIntentToActivity();
				}
				break;
			case R.id.qiye:

				if (bean != null) {
					if (bean.join_big_customer != 0) {
						if (bean.join_big_customer == -1) {
							get_big_customer_invite_info(true);
						} else {
							intent = new Intent(MyAppcation.getMyAppcation(), BigCustomerActivity.class);
							intent.putExtra("bean", bean);
							startActivity(intent);
						}
					} else {
						intent = new Intent(MyAppcation.getMyAppcation(), CompanyUserActivity.class);
						startActivity(intent);
					}
				} else {
					get_big_customer_invite_info(true);
				}

				break;
			case R.id.tuijian:
				intent = new Intent(MyAppcation.getMyAppcation(), MyShareActivity.class);
				startActivity(intent);
				break;
			case R.id.problem:
				intent = new Intent(MyAppcation.getMyAppcation(), QuestionListActivity.class);
				startActivity(intent);
				break;
			case R.id.setting:

				intent = new Intent(MyAppcation.getMyAppcation(), SettingActivity.class);
				startActivity(intent);
				break;

			default:
				break;
			}
		} else {
			intent = new Intent(MyAppcation.getMyAppcation(), LoginActivity.class);
			startActivity(intent);
		}
	}

	/**
	 * 开始跳转
	 */
	private void startIntentToActivity() {
		Get_user_information_statusRequest requestBean = new Get_user_information_statusRequest();
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				promptDialog.dismiss();
				statusBean = JSON.parseObject(response, UserInformationStatusBean.class);
				switch (statusBean.user_information_status) {
				case -1:
					status.setText("待认证");
					status.setTextColor(Color.parseColor("#f66767"));

					intent = new Intent(MyAppcation.getMyAppcation(), AuthorizeActivity.class);
					intent.putExtra("bean", statusBean);
					startActivity(intent);
					break;
				case 0:

					status.setText("未通过");
					status.setTextColor(Color.parseColor("#f66767"));
					intent = new Intent(MyAppcation.getMyAppcation(), AuthorizeActivity.class);
					intent.putExtra("bean", statusBean);
					startActivity(intent);
					break;
				case 1:

					status.setText("已认证");
					status.setTextColor(Color.parseColor("#999999"));
					intent = new Intent(MyAppcation.getMyAppcation(), AuthorizeCompleteActivity.class);
					startActivity(intent);
					break;
				case 2:

					status.setText("待审核");
					status.setTextColor(Color.parseColor("#999999"));
					intent = new Intent(MyAppcation.getMyAppcation(), AuthorizeVerifyActivity.class);
					startActivity(intent);
					break;
				default:
					break;
				}
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});

	}

	public void setData() {

		if (view != null)
			initData();
	}

	/**
	 * flag = 0登录页面和更改个人信息页面调用退出大客户页面
	 */

	@Override
	public Object dataChang(int flag, Object data) {

		switch (flag) {
		case F_Main.DEFAULT:
			setData();
			break;
		case F_Main.AUTHORIZE:
			get_user_information_statusRequest();
			break;

		default:
			break;
		}

		return super.dataChang(flag, data);
	}

}
